package br.com.android.modelo;

import java.util.List;

import br.com.android.R;
import br.com.android.persistencia.manipularDados;
import br.com.android.productivecenter.EditarCompromissoActivity;
import br.com.android.productivecenter.EditarDocumentoActivity;
import br.com.android.productivecenter.NovoCompromissoActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CalendarioAdapter extends BaseAdapter {
	
	private Context context;
	private List<Calendario> listaComp;
	private manipularDados persistencia;
	public String userName;
	
	public CalendarioAdapter(Context context, List<Calendario> listaComp, manipularDados persistencia, String userName) {
		this.context = context;
		this.listaComp = listaComp;
		this.persistencia = persistencia;
		this.userName = userName;
	}
	
    @Override
    public int getCount() {
        return listaComp.size();
    }

    @Override
    public Object getItem(int position) {
        return listaComp.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    	Calendario compromisso = listaComp.get(position);
    	
    	LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	View view = inflater.inflate(R.layout.listview_compromissos, null);
    	
        view.setClickable(true);
        view.setFocusable(true);
    	
    	TextView DataCompromissoF = (TextView) view.findViewById(R.id.DataCompromissoF);
    	DataCompromissoF.setText(compromisso.getData());
    	TextView TituloCompromissoF = (TextView) view.findViewById(R.id.TituloCompromissoF);
    	TituloCompromissoF.setText("   " + compromisso.getTitulo());
    	TextView HoraCompromissoF = (TextView) view.findViewById(R.id.HoraCompromissoF);
    	HoraCompromissoF.setText(compromisso.getHora());
    	
        view.setOnClickListener(new View.OnClickListener() {
            @Override
           public void onClick(View v) {
            	Calendario compromisso = listaComp.get(position);
            	Dialogs.imprimirMensagem(context, "Visualização Compromisso", 
            							 compromisso.getTitulo() + "\n\n" + "Data: " +
            							 compromisso.getData() + "\n\n" + "Hora "+ 
            							 compromisso.getHora() + "\n\n" + "Descrição: " +
            							 compromisso.getDescricao() + "\n");
            }

          });
        
        view.setOnLongClickListener(new View.OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View arg0) {
				Calendario compromisso = listaComp.get(position);
				
				Intent i = new Intent(context, EditarCompromissoActivity.class); 
				
				i.putExtra("nome", compromisso.getTitulo());
				i.putExtra("descricao", compromisso.getDescricao());
				i.putExtra("hora", compromisso.getHora());
				i.putExtra("data", compromisso.getData());
				i.putExtra("nelson", persistencia);
				i.putExtra("userName",userName);
				
				context.startActivity(i);
				
             new AlertDialog.Builder(context).setTitle(compromisso.getTitulo()).show();
             return false;
			}
		});

    	return view;
    }
}
